import request from '@/utils/request';
import { baseurl } from './baseurl';
import { hasIsSw,isLianxiSite,isCustome} from '../utils/utils';;
export interface addQuestionParams {
  types: string;
  type: string;
  complexity: string;
  content: string;
  a: string;
  b: string;
  c: string;
  d: string;
  e: string;
  f: string;
  answer: string;
  answer_that: string;
  is_audit: string;
  subject: any;
  img: any;
}

export async function addQuestion(params: addQuestionParams) {
  /* 添加试题 */
  const data = new FormData();
  data.append('types', params.types);
  data.append('type', params.type);
  data.append('complexity', params.complexity);
  data.append('content', params.content);
  data.append('subject', params.subject);
  if (params.type === '1' || params.type === '2') {
    data.append('a', params.a);
    data.append('b', params.b);
    data.append('c', params.c);
    data.append('d', params.d);
    data.append('e', params.e);
    data.append('f', params.f);
  }
  data.append('answer', params.answer);
  data.append('answer_that', params.answer_that);
  data.append('is_audit', params.is_audit);
  data.append('img', params.img);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/question_bank_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/question_bank`, {
    method: 'POST',
    data,
  });
}

export interface getQuestionListParams {
  page: string;
  types: string;
  type: string;
  content: string;
  subject: any;
  complexity: string;
  num: string;
}

export async function getQuestionList(params: getQuestionListParams) {
  /* 获取试题 */
  const data = new FormData();
  let is_sw = '0';
  if(location.href.includes('tzExamInfo')){
    is_sw='1'
  }else{
    is_sw='2'
  }
  data.append('page', params.page);
  data.append('types', params.types);
  data.append('type', params.type);
  data.append('content', params.content);
  data.append('subject', params.subject);
  data.append('num', params.num);
  data.append('complexity', params.complexity);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', is_sw);
    }else{
      data.append('is_sw',is_sw);
    }
  }
  if(isCustome()&&is_sw==='1'){
    return request(`${baseurl}index/index/question_list_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/question_list`, {
    method: 'POST',
    data,
  });
}

export interface getAuditsListParams {
  page: string;
  types: string;
  type: string;
  content: string;
  subject: any;
  complexity: string;
}

export async function getAuditsList(params: getAuditsListParams) {
  /* 获取禁用试题 */
  const data = new FormData();
  data.append('page', params.page);
  data.append('types', params.types);
  data.append('type', params.type);
  data.append('content', params.content);
  data.append('subject', params.subject);
  data.append('complexity', params.complexity);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/audits_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/audits`, {
    method: 'POST',
    data,
  });
}

export interface getQuestionDetailParams {
  id: string;
}

export async function getQuestionDetail(params: getQuestionDetailParams) {
  /* 获取单个试题详情 */
  const data = new FormData();
  data.append('id', params.id);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/question_detail_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/question_detail`, {
    method: 'POST',
    data,
  });
}

export interface editQuestionParams {
  id: string;
  types: string;
  type: string;
  complexity: string;
  content: string;
  a: string;
  b: string;
  c: string;
  d: string;
  e: string;
  f: string;
  answer: string;
  answer_that: string;
  is_audit: string;
  subject: string;
  img: any;
  old_img: string;
}

export async function editQuestion(params: editQuestionParams) {
  /* 编辑试题 */
  const data = new FormData();
  data.append('id', params.id);
  data.append('types', params.types);
  data.append('type', params.type);
  data.append('complexity', params.complexity);
  data.append('content', params.content);
  data.append('subject', params.subject);
  if (params.type === '1' || params.type === '2') {
    data.append('a', params.a);
    data.append('b', params.b);
    data.append('c', params.c);
    data.append('d', params.d);
    data.append('e', params.e);
    data.append('f', params.f);
  }
  data.append('answer', params.answer);
  data.append('answer_that', params.answer_that);
  data.append('is_audit', params.is_audit);
  data.append('img', params.img || '');
  data.append('old_img', !params.img ? params.old_img : '');
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/up_question_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/up_question`, {
    method: 'POST',
    data,
  });
}

export interface deleteQuestionParams {
  id: string;
}

export async function deleteQuestion(params: deleteQuestionParams) {
  /* 删除试题 */
  const data = new FormData();
  data.append('id', params.id);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/del_question_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/del_question`, {
    method: 'POST',
    data,
  });
}

export interface disableQuestionParams {
  is_audit: string;
  id: string;
}

export async function disableQuestion(params: disableQuestionParams) {
  /* 启用禁用试题 */
  const data = new FormData();
  data.append('id', params.id);
  data.append('is_audit', params.is_audit);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/is_audit_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/is_audit`, {
    method: 'POST',
    data,
  });
}

export interface batchUploadQuestionParams {
  doc: string;
  types: string;
  subject: string;
}
export async function batchUploadQuestion(params: batchUploadQuestionParams) {
  /* 批量导入 */
  const data = new FormData();
  data.append('doc', params.doc);
  data.append('types', params.types);
  data.append('subject', params.subject);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/to_question_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/to_question`, {
    method: 'POST',
    data,
  });
}

export interface operationQuestionBankParams {
  types: string;
  subject: string;
  type: string;
  q_type: string;
}
export async function operationQuestionBank(params: operationQuestionBankParams) {
  /* 批量操作题库 */
  const data = new FormData();
  data.append('type', params.type);
  data.append('types', params.types);
  data.append('q_type', params.q_type);
  data.append('subject', params.subject);
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  if(isCustome()){
    return request(`${baseurl}index/index/operation_question_optional`, {
      method: 'POST',
      data,
    });
  }
  return request(`${baseurl}index/index/operation_question`, {
    method: 'POST',
    data,
  });
}

export async function statisticsQuestionBank() {
  /* 统计分析题库 */
  const data = new FormData();
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  return request(`${baseurl}index/index/statistics_question`, {
    method: 'POST',
    data
  });
}
export async function statisticsQuestionBankOptional() {
  /* 统计分析题库 */
  const data = new FormData();
  if(hasIsSw()){
    if(location.href.includes('tzExamInfo')){
      data.append('is_sw', '1');
    }else{
      data.append('is_sw','2');
    }
  }
  return request(`${baseurl}index/index/statistics_question_optional`, {
    method: 'POST',
    data
  });
}
